package com.qq2008.game.stzb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qq2008.game.stzb.jdo.Bag;
import com.qq2008.game.stzb.jdo.Goods;
import com.qq2008.game.stzb.jdo.Role;
import com.qq2008.game.stzb.jdo.Shop;
import com.qq2008.game.stzb.mapper.BagMapper;
import com.qq2008.game.stzb.mapper.GoodMapper;
import com.qq2008.game.stzb.mapper.ShopMapper;
import com.qq2008.game.stzb.service.impl.RolePlusServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/mojie")
public class ShopController extends BaseController {

    @Resource
    private ShopMapper shopMapper;

    @Resource
    private GoodMapper goodMapper;

    @Resource
    private BagMapper bagMapper;

    @Autowired
    private RolePlusServiceImpl rolePlusService;

    /**
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("shopList")
    public String shop(Model model, HttpSession session) {
        commonShopList(model, session);
        return "shop";
    }

    @RequestMapping("shopDetail")
    public String shopDetial(Model model, HttpSession session, Integer goodId) {
        commonShopList(model, session);
        commonShopDetail(model, goodId);
        return "shop";
    }

    @RequestMapping("shopBuy")
    public String shopBuy(Model model, HttpSession session, Integer goodId) {
        commonShopList(model, session);
        commonShopDetail(model, goodId);
        Role role = getSessionRole(session);
        Shop shop = shopMapper.selectOne(new LambdaQueryWrapper<Shop>().eq(Shop::getMallGoodId, goodId));


        Integer mallPrice = shop.getMallPrice();//价格
        Integer money = role.getMoney();//余额

        if (money < mallPrice) {
            model.addAttribute("result", "余额不足!");
            return "shop";
        }

        role.setMoney(money - mallPrice);
        rolePlusService.update(role, new LambdaQueryWrapper<Role>().eq(Role::getUserId, role.getUserId()));
        Bag bag = new Bag().builder()
                .userId(role.getUserId())
                .bagGoodId(goodId)
                .bagGoodType(shop.getMallType())
                .bagGoodName(shop.getMallGoodName())
                .build();
        int insert = bagMapper.insert(bag);
        if (insert > 0) {
            model.addAttribute("result", "购买成功!");
        } else {
            model.addAttribute("result", "购买失败!");
        }
        commonShopList(model, session);
        commonShopDetail(model, goodId);
        return "shop";
    }


    void commonShopList(Model model, HttpSession session) {
        Role role = getSessionRole(session);
        model.addAttribute("money", role.getMoney());
        List<Shop> shops = shopMapper.selectList(null);
        model.addAttribute("shopList", shops);
    }

    void commonShopDetail(Model model, Integer goodId) {
        Shop shop = shopMapper.selectOne(new LambdaQueryWrapper<Shop>().eq(Shop::getMallGoodId, goodId));
        model.addAttribute("shopDetail", shop);
        Goods goods = goodMapper.selectOne(new LambdaQueryWrapper<Goods>().eq(Goods::getId, goodId));
        model.addAttribute("good", goods);
    }

}
